//
//  VVHomeRetWeetView.swift
//  Weibo
//
//  Created by 王惠平 on 2017/3/31.
//  Copyright © 2017年 Will Wang. All rights reserved.
//

import UIKit
import SnapKit

class VVHomeRetWeetView: UIView {

    //懒加载控件
    fileprivate lazy var contenLabel: UILabel = {
        
       let label = UILabel(fonSize: 13, textColor: UIColor.lightGray, text: "我要和你一起去远行,在那春天到来的时候")
        
       //多行显示
        label.numberOfLines = 0
        
        return label
        
    }()
    
    //配图
    fileprivate lazy var pictureView: VVHomePictureView = {
        
        let picView = VVHomePictureView()
        
        picView.backgroundColor = self.backgroundColor
        
       return picView
        
    }()
    
    //记录转发微博底部约束
    var reweetButtomConstraint: Constraint?
    
    //绑定数据
    var statusViewModel: VVStatusViewModel? {
        
        didSet {
            
            //转发微博内容
            contenLabel.attributedText = statusViewModel?.retweetAttributedString
            
            //先把之前的转发微博的底部约束卸载掉,
            reweetButtomConstraint?.deactivate()
            
            if let picUrls = statusViewModel?.status?.retweeted_status?.pic_urls, picUrls.count > 0 {
                
                
                //重新创建约束
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    
                  reweetButtomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                
                //有配图
                pictureView.picUrls = picUrls
                
            } else {
                
                //没有视图
                pictureView.isHidden = true
                
                self.snp.makeConstraints({ (make) in
                    
                    reweetButtomConstraint = make.bottom.equalTo(contenLabel).offset(HomeCellMargin).constraint
                })
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
    
        setupUI()
    }
    
    //支持xib的写法
    required init?(coder aDecoder: NSCoder) {
       // fatalError("init(coder:) has not been implemented")
     super.init(coder: aDecoder)
    }
    
    //添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor(white: 0.95, alpha: 1.0)
        
        addSubview(contenLabel)
        addSubview(pictureView)
        
        contenLabel.snp.makeConstraints { (make) in
            
            make.top.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureView.snp.makeConstraints { (make) in
            
            make.top.equalTo(contenLabel.snp.bottom).offset(HomeCellMargin)
            make.left.equalTo(contenLabel)
            
            //暂时写上
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        //关键约束
        self.snp.makeConstraints { (make) in
            
           reweetButtomConstraint = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
            
        }
        
    }
    
}
